﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using ARR.Entities;
using ARR.UI.CategoriesService;
using DDF.Entity;

namespace ARR.UI.Models
{
    public interface ICategoryMasterModel : IModelBase
    {
        ObservableCollection<Category> Categories { get; set; }

        ObservableCollection<CategoryUniverse> Universes { get; set; }

        ObservableCollection<CategoryRole> Roles { get; set; }

        void Save();

        void LoadCategories();
    }


    public class CategoryMasterModel : ModelBase, ICategoryMasterModel
    {
        public CategoryMasterModel()
        {
            Categories = new ObservableCollection<Category>();
            Roles = new ObservableCollection<CategoryRole>();
            Universes = new ObservableCollection<CategoryUniverse>();

            LoadUniverses();
            LoadRoles();
            LoadCategories();
        }

        public ObservableCollection<Category> Categories { get; set; }

        public ObservableCollection<CategoryUniverse> Universes { get; set; }

        public ObservableCollection<CategoryRole> Roles { get; set; }

        public void Save()
        {
            var toSave = Categories.Where(x => x.GetEntityState() != EntityState.Original);

            if (toSave.Any())
            {
                CategoriesServiceClient service = new CategoriesServiceClient();

                try
                {
                    service.SaveHierarchy(toSave.ToList());
                    service.Close();
                }
                catch
                {
                    service.Abort();
                    throw;
                }
            }
        }

        private void LoadRoles()
        {
            CategoriesServiceClient service = new CategoriesServiceClient();

            try
            {
                Roles = new ObservableCollection<CategoryRole>(service.GetCategoryRoles());
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }
        }

        private void LoadUniverses()
        {
            CategoriesServiceClient service = new CategoriesServiceClient();

            try
            {
                Universes = new ObservableCollection<CategoryUniverse>(service.GetCategoryUniverses());
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }
        }

        public void LoadCategories()
        {
            List<Category> categories = new List<Category>();
            CategoriesServiceClient service = new CategoriesServiceClient();

            try
            {
                categories = service.GetAll();
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }

            foreach (var categoryLvl1 in categories.Where(x => x.Level == 1))
            {
                categoryLvl1.SubCategories = new List<Category>();
                categoryLvl1.SubCategories.AddRange(categories.Where(x => x.ParentId == categoryLvl1.Id));

                foreach (var categoryLvl2 in categoryLvl1.SubCategories.Where(x => x.Level == 2))
                {
                    categoryLvl2.SubCategories = new List<Category>();
                    categoryLvl2.SubCategories.AddRange(categories.Where(x => x.ParentId == categoryLvl2.Id));

                    categoryLvl2.ChangeState(EntityState.Original);
                }

                categoryLvl1.ChangeState(EntityState.Original);
            }

            Categories = new ObservableCollection<Category>(categories.Where(x => x.Level == 1).OrderBy(x => x.Name));

        }
    }


    public class CategoryMasterModelMock : MockModelBase, ICategoryMasterModel
    {
        public CategoryMasterModelMock()
        {
            Categories = new ObservableCollection<Category>();
            Roles = new ObservableCollection<CategoryRole>();
            Universes = new ObservableCollection<CategoryUniverse>();

            LoadCategories();
        }

        public ObservableCollection<Category> Categories { get; set; }

        public ObservableCollection<CategoryUniverse> Universes { get; set; }

        public ObservableCollection<CategoryRole> Roles { get; set; }

        public void Save()
        {

        }

        public void LoadCategories()
        {
            Categories.Add(new Category { Code = "001211111111", Name = "Category1" });
            Categories.Add(new Category { Code = "001211111122", Name = "Category2" });
            Categories.Add(new Category { Code = "001211111133", Name = "Category3" });
            Categories.Add(new Category { Code = "001211111114", Name = "Category4" });
        }
    }
}
